
package com.homeos;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.SASLAuthentication;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.filter.MessageTypeFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.packet.Presence;

import org.jivesoftware.smack.packet.Packet;







public class Home extends Activity {
	
	
	
    /** Called when the activity is first created. */
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
      
        
        
    }
    @Override
    public void onResume() {
        super.onResume();
        setContentView(R.layout.main);
        if(!isOnline())
        {
     	   AlertDialog.Builder alert = new AlertDialog.Builder(this);
        	alert.setTitle("Alert!");
        	alert.setMessage("No Internet Connectivity !\nCannot connect to Home OS");
        	alert.setIcon(R.drawable.icon);
        	alert.setPositiveButton("Back",
        	 new DialogInterface.OnClickListener() {
        	  public void onClick(DialogInterface dialog, int id) {
        		  
       	   // Toast.makeText(view.getContext(), "Success", Toast.LENGTH_SHORT).show();
        	  }
        	 });
        
        	alert.show();	
        	
     	   
     	   
        }
         
    }
    
    public void Login(View view){
    	
    	 if(!isOnline())
         {
      	   AlertDialog.Builder alert = new AlertDialog.Builder(this);
         	alert.setTitle("Alert!");
         	alert.setMessage("No Internet Connectivity !\nCannot connect to Home OS");
         	alert.setIcon(R.drawable.icon);
         	alert.setPositiveButton("Back",
         	 new DialogInterface.OnClickListener() {
         	  public void onClick(DialogInterface dialog, int id) {
         	  }
         	 });
         
         	alert.show();	
         	return;
      	   
      	   
         }
    	 
    	 
    	Bundle b = new Bundle();
    	EditText host = (EditText) findViewById(R.id.Server);
    	
    	Log.d("INPUT user",host.getText().toString());
    	
    	if(host.getText().toString().equals("gmail.com"))
    	{
    		b.putString("host","talk.google.com");
    		b.putString("serviceName","gmail.com"); 
    	}
    	else
    	{
    		b.putString("host",host.getText().toString());
    		b.putString("serviceName","");
    	}
    
    	
    	
    	host = (EditText) findViewById(R.id.Port);
    	b.putString("port", host.getText().toString());
    	
    
    	host = (EditText) findViewById(R.id.Username);
    	b.putString("username", host.getText().toString());

    	host = (EditText) findViewById(R.id.Password);
    	b.putString("password", host.getText().toString());
    	

    	Intent myIntent = new Intent(view.getContext(),HomeOS.class);
    	 myIntent.putExtras(b);
         startActivity(myIntent);
         finish();
         
    	
    }
    
    public boolean isOnline() {
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo ni = cm.getActiveNetworkInfo();
        if (ni!=null && ni.isAvailable() && ni.isConnected()) {
            return true;
        } else {
            return false; 
        }
    }




}